package com.imooc.mall.controller;

import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.common.Constant;
import com.imooc.mall.exception.ImoocMallException;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.pojo.User;
import com.imooc.mall.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author yijiu
 * @version 1.0
 * @description: 用户Controller
 * @date 2022/2/20 15:28
 */
@RestController
public class UserController {


    @Autowired
    UserService userService;

    @GetMapping("/test")
    public User personalPage(){
        return userService.getUser();
    }


    /**
    * @description: 注册新用户
    * @author
    * @date
    * @version 1.0
    */
    @ApiOperation("后台用户注册")
    @PostMapping("/register")
    public ApiRestResponse register(@RequestParam("userName") String userName,@RequestParam("password") String password) throws ImoocMallException {

        //使用StringUtils工具类进行用户名判空
        if (StringUtils.isEmpty(userName)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_PASSWORD);
        }
        //密码长度不能少于8位
        if (password.length() < 8) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_TOO_SHORT);
        }
        //进行用户注册
        userService.register(userName, password);
        //返回操作成功
        return ApiRestResponse.success();
    }

    @ApiOperation("后台用户登录")
    @PostMapping("/login")
    public ApiRestResponse login(@RequestParam("userName") String userName, @RequestParam("password") String password, HttpSession session) throws ImoocMallException {
        //使用StringUtils工具类进行用户名判空
        if (StringUtils.isEmpty(userName)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_PASSWORD);
        }
        User user = userService.login(userName, password);
        //设置密码为null 避免密码泄露
        user.setPassword(null);
        session.setAttribute(Constant.IMOOC_MALL_USER,user);
        return ApiRestResponse.success(user);
    }

    @ApiOperation("后台用户签名更新")
    @PostMapping("user/update")
    public ApiRestResponse updateUserInfo(HttpSession session,@RequestParam String signature) throws ImoocMallException {
        User currentUser = (User)session.getAttribute(Constant.IMOOC_MALL_USER);
        if (currentUser == null) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGIN);
        }
        User user = new User();
        user.setId(currentUser.getId());
        user.setPersonalizedSignature(signature);
        userService.updateInformation(user);  //修改bug 之前user 写的是currentUser
        return ApiRestResponse.success();

    }

    @ApiOperation("后台用户退出登录")
    @PostMapping("/user/logout")
    public ApiRestResponse logOut(HttpSession session) {
        session.removeAttribute(Constant.IMOOC_MALL_USER);
        return ApiRestResponse.success();
    }

    @ApiOperation("后台管理员登录")
    @PostMapping("/adminLogin")
    public ApiRestResponse adminLogin(@RequestParam("userName") String userName, @RequestParam("password") String password, HttpSession session) throws ImoocMallException {
        //使用StringUtils工具类进行用户名判空
        if (StringUtils.isEmpty(userName)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_PASSWORD);
        }
        User user = userService.login(userName, password);
        boolean adminRole = userService.checkAdminRole(user);
        if (adminRole) {
            //是管理员
            //设置密码为null 避免密码泄露
            user.setPassword(null);
            //session中存储用户信息
            session.setAttribute(Constant.IMOOC_MALL_USER,user);
            return ApiRestResponse.success(user);
        } else {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
        }
    }

}
